Ext.ns('FacilityRegiView');

FacilityRegiView = Ext.extend(Ext.Panel, {
	topbar : null,
	searchPanel : null,
	gridPanel : null,
	store : null,
	constructor : function(_cfg) {
	    
		Ext.applyIf(this, _cfg);
		this.initUIComponents();
		FacilityRegiView.superclass.constructor.call(this, {
					id : 'FacilityRegiView',
					title : '设备注册',
					region : 'center',
					layout : 'border',
					items : [this.searchPanel, this.gridPanel]
				});

	}, 

	initUIComponents : function() {
		
		this.searchPanel = new Ext.FormPanel({
			id : 'FacilityRegiSearchFormPanel',
			height : 70,
			region : 'north',
			frame : false,
			border : false,
			padding : '10',
			layout : 'column',
			title : '查询条件',
			labelWidth : 60,
			layoutConfig : {
				padding : '5',
				align : 'middle'
			},
			defaults : {
				border : false,
				columnWidth : 0.25,
				layout : 'form',
				width : 120,
				padding : '0 0 0 10'
			},
			items : [{
						items : [{
									fieldLabel : 'MAC地址',
									xtype : 'textfield',
									name : 'Q_mac_S_LK'
								}]
					}, {
						items : [{
									fieldLabel : '领用人',
									xtype : 'textfield',
									name : 'Q_useBy_S_LK'
								}]
					}, {
						items : [{
							fieldLabel : '状态',
							id : 'statusId',
							hiddenName : 'Q_status_S_LK',
							xtype : 'combo',
							mode : 'local',
							width : 110,
							editable : false,
							triggerAction : 'all',
							store : [ [ '9','可用' ], ['0','不可用'] ],
							value : '9'
						}]
			      }, {
						items : [new Ext.Button({
											iconCls : 'search',
											text : '查询',
											handler : function() {	
								                var searchPanel = Ext.getCmp('FacilityRegiSearchFormPanel');
												var gridPanel = Ext.getCmp('FacilityRegiGrid');
												if (searchPanel.getForm().isValid()) {
													$search({
														searchPanel :searchPanel,
														gridPanel : gridPanel
													});
												}
											}
										})]
					}]
		});
		
		
		this.store = new Ext.data.Store({
			proxy : new Ext.data.HttpProxy({
						url : __ctxPath
								+ '/pdaweb/listDeviceReg.do'
					}),
			reader : new Ext.data.JsonReader({
						root : 'result',
						totalProperty : 'totalCounts',
						fields : ['id','mac', 'useBy', 'createddt', 'status']
					}),
			remoteSort : true
		});// end of the store
		this.store.load({
			params : {
			start : 0, limit : 25 
			}
		});
/*
 * 
 * this.store = new Ext.data.JsonStore({
 * 
 * proxy : new Ext.data.HttpProxy({ url : __ctxPath + '/pdaweb/listDeviceReg.do'
 * }), reader : new Ext.data.JsonReader({}), root : 'result', totalProperty :
 * 'totalCounts', fields : [{ name : 'id', type : 'int' }, 'mac', 'useBy',
 * 'createddt', 'status'],
 * 
 * remoteSort : true
 *  // fields : ['amId', 'districtId', 'buildingId', // 'aparmentName', 'attr0',
 * 'attr1'], // data : [['AA', 'AA0', 'AA0', 'AA0', 'AA0', 'AA0'], // ['AA',
 * 'AA0', 'AA0', 'AA0', 'AA0', 'AA0']] });
 */
		/*
		 * this.store.setDefaultSort('meterId', 'desc'); this.store.load({
		 * params : { start : 0, limit : 25 } });
		 */

		var cm = new Ext.grid.ColumnModel({
			defaults : {
				sortable : true,
				menuDisabled : false
			},
			columns : [new Ext.grid.RowNumberer(), /*
													 * { header : '设备编号', width :
													 * 120, dataIndex : 'id' },
													 */ {
						header : 'mac地址',
						width : 100,
						dataIndex : 'mac'
					}, {
						header : '领用人',
						width : 100,
						dataIndex : 'useBy'
					}, {
						header : '注册日期',
						width : 100,
						dataIndex : 'createddt',
						renderer:function(v){
							return new Date(v).format('Y-m-d');
						}
					}, {
						header : '状态',
						dataIndex : 'status',
						width : 100,
						renderer:function(v){// 渲染函数
							return v=="9"?"可用":"不可用";
						}
					}, {
						header : '管理',
						dataIndex : 'id',
						width : 50,
						sortable : false,
						renderer : function(value, metadata, record, rowIndex,
								colIndex) {
							  var str='';
							  str += '&nbsp;<button title="编辑" value=" " class="btn-edit" '
										+ 'onclick="new FacilityRegiForm({altMode : \'true\',id:\''+record.data.id
										+'\'}).show();">&nbsp</button>';
	                           str += '&nbsp;<button title="删除" value=" " class="btn-delete" onclick="FacilityRegiView.remove('
											+ record.data.id+')">&nbsp</button>';
	                    
							  return str;
						}
					}]
		});

		this.topbar = new Ext.Toolbar({
					height : 30,
					bodyStyle : 'text-align:left',
					items : [new Ext.Button({
								iconCls : 'btn-add',
								text : '新增注册设备',
								handler : function() {
									new FacilityRegiForm().show();
								}
							})]
				});

		this.gridPanel = new Ext.grid.GridPanel({
					id : 'FacilityRegiGrid',
					tbar : this.topbar,
					region : 'center',
					store : this.store,
					loadMask : true,
					autoHeight : true,
					cm : cm,
					viewConfig : {
						forceFit : true,
						enableRowBody : false
					},
					bbar : new Ext.PagingToolbar({
								pageSize : 25,
								store : this.store,
								displayInfo : true,
								displayMsg : '当前显示从{0}至{1}， 共{2}条记录',
								emptyMsg : "当前没有记录"
							})
				});
	}
});

/**
 * 删除记录
 */
FacilityRegiView.remove = function(id) {
	var grid = Ext.getCmp("FacilityRegiGrid");
	Ext.Msg.confirm('信息确认', '您确认要删除该记录吗？', function(btn) {
				if (btn == 'yes') {
					Ext.Ajax.request({
								url : __ctxPath
										+ '/pdaweb/multiDelDeviceReg.do',
								params : {
									ids : id
								},
								method : 'post',
								success : function() {
									Ext.ux.Toast.msg("信息提示", "成功删除所选记录！");
									grid.getStore().reload({
												params : {
													start : 0,
													limit : 25
												}
											});
								}
							});
				}
			});
};